package club.zhcs.captrue.module.auth;

import java.util.List;

import org.nutz.dao.entity.Record;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import club.zhcs.capture.bean.auth.Permission;
import club.zhcs.capture.bean.auth.RolePermission;
import club.zhcs.capture.bean.auth.UserPermission;
import club.zhcs.capture.biz.auth.impl.PermissionService;
import club.zhcs.capture.biz.auth.impl.RolePermissionService;
import club.zhcs.capture.biz.auth.impl.UserPermissionService;
import club.zhcs.capture.vo.Consts.PermissionGroup;

import com.dgj.nutz.module.base.AbstractBaseModule;
import com.dgj.utils.entries.Result;
import com.dgj.utils.web.pager.Pager;

/**
 * @author 黄敏 create at 2015年9月14日
 */
@At("/permission")
public class PermissionModule extends AbstractBaseModule {

	@Inject
	private PermissionService permissionService;

	@Inject
	private RolePermissionService rolePermissionService;

	@Inject
	private UserPermissionService userPermissionService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.nutz.module.base.AbstractBaseModule#_getNameSpace()
	 */
	@Override
	public String _getNameSpace() {
		return PermissionGroup.USER_ADMIN.getNameSpace();
	}

	@At
	@Ok("vel:permission/list.html")
	public Result list(int page) {
		page = _fixPage(page);
		Pager<Record> pager = permissionService.findByPage(page);
		pager.setUrl(_base() + "/permission/list");
		return Result.success().addData("pager", pager);
	}

	@At
	@GET
	@Ok("vel:/permission/add.html")
	public Result add() {
		return Result.success().addData("permissionGroup", PermissionGroup.values());
	}

	@At
	@POST
	public Result add(@Param("..") Permission permission) {
		Permission p = permissionService.addPermission(permission);
		if (p == null) {
			return Result.fail("添加权限失败");
		}
		return Result.success();
	}

	@At("/delete/*")
	@POST
	public Result delete(int id) {
		/*
		 * 先查询是否已经授权,已授权则不能删除
		 */
		if (id <= 0) {
			return Result.fail("权限id不存在");
		}
		List<RolePermission> rolePermissions = rolePermissionService.findByPermissionId(id);
		List<UserPermission> userPermission = userPermissionService.findByPermissionId(id);
		if (rolePermissions.size() > 0 || userPermission.size() > 0) {
			return Result.fail("已授权,不能删除");
		}
		return permissionService.delete(id);
	}

	@At
	@POST
	public Result checkPermissionName(String name) {
		Permission permission = permissionService.findByName(name);
		if (permission != null) {
			return Result.fail("权限名已存在,请重新输入");
		}
		return Result.success();
	}

	@At
	@POST
	public Result checkUrl(String url) {
		Permission permission = permissionService.findByUrl(url);
		if (permission != null) {
			return Result.fail("权限url已存在,请重新输入");
		}
		return Result.success();
	}

	@At("/edit/*")
	@Ok("vel:permission/edit.html")
	public Result edit(int id) {
		Permission permission = permissionService.findById(id);
		return Result.success().addData("permissionGroup", PermissionGroup.values()).addData("permission", permission);
	}

	@At
	@POST
	public Result update(@Param("..") Permission permission) {
		return permissionService.update(permission, new String[] { "isMenu", "url", "hilightKey", "description", "menuGroup" });
	}

	@At
	@Ok("vel:permission/list.html")
	public Result search(int page, String key) {
		page = _fixPage(page);
		key = _fixSearchKey(key);
		Pager<Record> pager = permissionService.search(page, key);
		pager.setUrl(_base() + "/permission/search");
		pager.addParas("key", key);
		return Result.success().addData("pager", pager).addData("key", key);
	}

}
